package com.platform.system.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.base.PageInfo;
import com.platform.common.base.QueryHelpMybatisPlus;
import com.platform.common.utils.CacheKey;
import com.platform.common.utils.ConvertUtil;
import com.platform.common.utils.PageUtil;
import com.platform.common.utils.RedisUtils;
import com.platform.system.system.mapper.DictDetailMapper;
import com.platform.system.system.mapper.DictMapper;
import com.platform.system.system.model.Dict;
import com.platform.system.system.model.DictDetail;
import com.platform.system.system.model.dto.DictDetailDto;
import com.platform.system.system.model.dto.DictDetailQueryParam;
import com.platform.system.system.service.DictDetailService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jinjin
 * @date 2020-09-24
 */
@Service
@AllArgsConstructor
@CacheConfig(cacheNames = "dictDetail")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, DictDetail> implements DictDetailService {

    private final DictMapper dictMapper;
    private final RedisUtils redisUtils;

    @Override
    //@Cacheable
    public PageInfo<DictDetailDto> queryAll(DictDetailQueryParam query, Pageable pageable) {
        IPage<DictDetail> page = PageUtil.toMybatisPage(pageable);
        IPage<DictDetail> pageList = this.page(page, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(pageList, DictDetailDto.class);
    }

    @Override
    //@Cacheable
    public List<DictDetailDto> queryAll(DictDetailQueryParam query) {
        return ConvertUtil.convertList(this.list(QueryHelpMybatisPlus.getPredicate(query)), DictDetailDto.class);
    }

    @Override
    public List<DictDetailDto> getDictByName(String dictName) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dict::getName, dictName);
        Dict dict = dictMapper.selectOne(wrapper);
        List<DictDetailDto> ret = this.baseMapper.getDictDetailsByDictName(dictName);
        redisUtils.set(CacheKey.DICTDEAIL_DICTID + dict.getId(), ret);
        return ret;
    }

    @Override
    public PageInfo<DictDetailDto> getDictByName(String dictName, Pageable pageable) {
        IPage<DictDetailDto> page = PageUtil.toMybatisPage(pageable, true);
        return ConvertUtil.convertPage(this.baseMapper.getDictDetailsByDictName(dictName, page), DictDetailDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyById(DictDetailDto resources) {
        DictDetail detail = ConvertUtil.convert(resources, DictDetail.class);
        boolean ret = this.updateById(detail);
        // 清理缓存
        delCaches(detail.getDictId());
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(DictDetailDto resources) {
        DictDetail detail = ConvertUtil.convert(resources, DictDetail.class);
        detail.setDictId(resources.getDict().getId());
        boolean ret = this.save(detail);
        // 清理缓存
        delCaches(detail.getDictId());
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        DictDetail dictDetail = this.getById(id);
        boolean ret = this.removeById(id);
        // 清理缓存
        delCaches(dictDetail.getDictId());
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByDictId(Long id) {
        UpdateWrapper<DictDetail> wrapper1 = new UpdateWrapper<>();
        wrapper1.lambda().eq(DictDetail::getDictId, id);
        boolean ret = this.remove(wrapper1);
        delCaches(id);
        return ret;
    }

    private void delCaches(Long dictId) {
        redisUtils.del(CacheKey.DICTDEAIL_DICTID + dictId);
    }
}
